import type {
  CodeType,
  Patient,
  PatientList,
  User,
  UserInfo
} from '@/types/user'
import { request } from '@/utils/request'

// 密码登录
export const loginByEmailAndPassword = async (
  email: string,
  password: string
): Promise<User> => await request.post('/user/login', { email, password })

// 发送验证码
export const sendEmailCode = async (email: string) =>
  await request.get('/user/sendEmailCode/' + email)

// 验证码登录
// 验证码登录
export const loginByEmailAndVerifyCode = async (
  email: string,
  code: string
): Promise<User> =>
  await request.post('/user/login', { email, verifyCode: code })

export const registerByEmailAndVerifyCode = async (
  email: string,
  code: string,
  password: string
) => await request.post('/user/register', { email, verifyCode: code, password })

// 获取用户信息
export const getUserInfo = async (useId: string): Promise<UserInfo> =>
  await request.get('/user/info/' + useId)

// 获取患者列表信息
// 获取患者列表信息
export const getPatientList = async (userId: string): Promise<PatientList> =>
  await request.get('patient/list/' + userId)

// 添加患者
export const addPatient = async (userId: string, patient: Patient) => {
  const { name, idCard, gender, age, phone } = patient
  let genderValue
  if (typeof gender === 'number') {
    genderValue = gender === 0 ? '男' : '女'
  } else {
    genderValue = gender
  }
  return await request.post('patient/add', {
    userId,
    name,
    idCard,
    gender: genderValue,
    age,
    phone
  })
}

// 编辑患者
export const editPatient = async (id: string, patient: Patient) => {
  const { name, idCard, gender, age, phone } = patient
  console.log(gender)
  let genderValue
  if (typeof gender === 'number') {
    genderValue = gender === 0 ? '男' : '女'
  } else {
    genderValue = gender
  }
  return await request.put('patient/update', {
    id,
    name,
    idCard,
    gender: genderValue,
    age,
    phone
  })
}

// 删除患者
export const delPatient = (patientId: string) =>
  request.delete(`patient/del/${patientId}`)

// 患者详情
export const getPatientDetail = async (userId: string, patientId: string) =>
  await request.post(`patient/info`, { userId, patientId })

export const getUnreadMessageCount = (userId?: string) => {
  if (userId) {
    return request.get<number>(`chat/unread-count/${userId}`)
  }
  return request<number>('patient/message/unRead/all')
}

export const loginByQQ = (openId: string) =>
  request<User>('login/thirdparty', 'POST', {
    openId,
    source: 'qq'
  })

export const bindMobile = (data: {
  mobile: string
  code: string
  openId: string
}) => request<User>('login/binding', 'POST', data)
